import Dialog from '@vant/weapp/dialog/dialog';
import http from "../../../utils/http";
import {Api} from "../../../utils/api";
Page({
    data: {
        productInfo: {
            id: '',
            code: '',
            name: '',
            desc: '',
            image: '',
            price: '',
            salary: '',
            category: '',
            categoryName: ''
        },
        categoryList: [],
        unitList: [
            {id: 1, name: '个'},
            {id: 2, name: '件'},
            {id: 3, name: '套'},
            {id: 4, name: '箱'},
            {id: 5, name: '袋'},
            {id: 6, name: '千克'},
            {id: 7, name: '吨'},
        ],
        imageList: []
    },
    onLoad: function (options) {
        const category = options.category
        const categoryName = options.categoryName
        this.setData({'productInfo.category': category, 'productInfo.categoryName': categoryName})
        this.getCategoryList()
    },
    getCategoryList() {
        http.getRequest(Api.getCategoryList, {type: 0}, (res) => {
            this.setData({categoryList: res.data})
        })
    },
    onChangeField(e) {
        const field = e.currentTarget.dataset.field
        const value = e.detail
        this.setData({[`productInfo.${field}`]: value})
    },
    afterRead(event) {
        const { file } = event.detail;
        const token = wx.getStorageSync('token');
        const _this = this
        wx.uploadFile({
            url: Api.http + Api.uploadImage,
            filePath: file.url,
            name: 'file',
            formData: { type: 'material' , token},
            success(res) {
                const image = JSON.parse(res.data).data
                _this.setData({ 'productInfo.image': image, imageList: [{url: image}] });
            },
        });
    },
    onOversize() {
        Dialog.alert({ title: '提示', message: '文件大小不能超过 2MB' });
    },
    onDeleteImage() {
        this.setData({ 'productInfo.image': '', imageList: [] });
    },
    onStockChange(e) {
        this.setData({'productInfo.stock': e.detail})
    },
    onChangeCategory(e) {
        const categoryIndex = e.detail.value
        const categoryList = this.data.categoryList
        const category = categoryList[categoryIndex].id
        const categoryName = categoryList[categoryIndex].name
        this.setData({'productInfo.category': category, 'materialInfo.categoryName': categoryName})
    },
    onChangeUnit(e) {
        const unitIndex = e.detail.value
        const unitList = this.data.unitList
        const unit = unitList[unitIndex].name
        this.setData({'productInfo.unit': unit})
    },
    saveProduct() {
        const productInfo = this.data.productInfo
        if (productInfo.code === '') {
            Dialog.alert({message: '请输入商品编码'})
            return
        }
        if (productInfo.name === '') {
            Dialog.alert({message: '请输入商品名称'})
            return
        }
        if (productInfo.category === '') {
            Dialog.alert({message: '请选择商品分类'})
            return
        }
        http.postRequest(Api.saveProduct, productInfo, (res) => {
            Dialog.alert({message: '保存成功'}).then(() => {
                wx.navigateBack()
            })
        })
    }
});
